package edu.xhu.test;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import edu.xhu.api.id.IdClient;
import edu.xhu.api.video.VideoClient;
import edu.xhu.behavior.BehaviorApplication;
import edu.xhu.behavior.mapper.BiliUserCommentMapper;
import edu.xhu.model.behavior.pojos.BiliUserComment;
import edu.xhu.model.behavior.vo.CommentTree;
import edu.xhu.model.common.enums.IsFlagEnum;
import edu.xhu.model.video.pojos.BiliVideo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

@SpringBootTest(classes = BehaviorApplication.class)
@RunWith(SpringRunner.class)
public class addDataToComment {

    @Autowired
    private BiliUserCommentMapper testMapper;

    @Autowired
    private IdClient idClient;

    @Autowired
    private VideoClient videoClient;

    @Test
    public void addData() throws InterruptedException {
        JSONArray jsonArray = JSONUtil.parseArray(videoClient.videosListApi().getData().toString());
        List<BiliVideo> list = JSONUtil.toList(jsonArray, BiliVideo.class);
        long start = System.currentTimeMillis();
        System.out.println(list);
        List<BiliVideo> list1 = list.subList(0, 5);
        System.out.println(list1.size());
        List<BiliVideo> list2 = list.subList(5, 10);
        System.out.println(list2.size());
        List<BiliVideo> list3 = list.subList(10, 15);
        List<BiliVideo> list4 = list.subList(15, 20);
        List<BiliVideo> list5 = list.subList(20, 25);
        List<BiliVideo> list6 = list.subList(25, 30);
        List<BiliVideo> list7 = list.subList(30, 35);
        List<BiliVideo> list8 = list.subList(35, 40);
        List<BiliVideo> list9 = list.subList(40, 45);
        List<BiliVideo> list10 = list.subList(45, 50);
        List<BiliVideo> list11 = list.subList(50, 55);
        List<BiliVideo> list12 = list.subList(55, 60);
        List<BiliVideo> list13 = list.subList(60, 65);
        List<BiliVideo> list14 = list.subList(65, 70);
        List<BiliVideo> list15 = list.subList(70, 75);
        CompletableFuture<Boolean> asy1 = asy(list1);
        CompletableFuture<Boolean> asy2 = asy(list2);
        CompletableFuture<Boolean> asy3 = asy(list3);
        CompletableFuture<Boolean> asy4 = asy(list4);
        CompletableFuture<Boolean> asy5 = asy(list5);
        CompletableFuture<Boolean> asy6 = asy(list6);
        CompletableFuture<Boolean> asy7 = asy(list7);
        CompletableFuture<Boolean> asy8 = asy(list8);
        CompletableFuture<Boolean> asy9 = asy(list9);
        CompletableFuture<Boolean> asy10 = asy(list10);
        CompletableFuture<Boolean> asy11 = asy(list11);
        CompletableFuture<Boolean> asy12 = asy(list12);
        CompletableFuture<Boolean> asy13 = asy(list13);
        CompletableFuture<Boolean> asy14 = asy(list14);
        CompletableFuture<Boolean> asy15 = asy(list15);
        asy1.join();
        asy2.join();
        asy3.join();
        asy4.join();
        asy5.join();
        asy6.join();
        asy7.join();
        asy8.join();
        asy9.join();
        asy10.join();
        asy11.join();
        asy12.join();
        asy13.join();
        asy14.join();
        asy15.join();
        System.out.println(System.currentTimeMillis() - start + ":ms");

    }

    private CompletableFuture<Boolean> asy(List<BiliVideo> list) {
        return CompletableFuture.supplyAsync(() -> {
            for (BiliVideo video : list) {
                for (int i = 0; i < 200; i++) {
                    //root
                    BiliUserComment root = new BiliUserComment();
                    long id = Long.parseLong(idClient.getSnowflakeDistributedId().getData().toString());
                    root.setCommentId(id);
                    root.setVideoId(video.getVideoId());
                    root.setRootId(null);    //插根评论
                    root.setParentId(null);  //插父评论
                    root.setToUserId(null);   //表示根评论用户
                    root.setUserId(274892974465220608L);     //表示评论用户
                    root.setContent("根评论:" + i);
                    root.setLove(new Random().nextInt(9999) + 1);    //9999内随机数
                    root.setBad(new Random().nextInt(77) + 1);    //100内随机数
                    root.setCreateTime(new Date());
                    root.setIsTop(IsFlagEnum.NO.getCode());
                    root.setIsDelete(IsFlagEnum.NO.getCode());
                    root.setIsLike(IsFlagEnum.NO.getCode());
                    root.setTotalCommentSize(1);
                    root.setDeep(0);
                    //son
                    BiliUserComment son = new BiliUserComment();
                    long sonId = Long.parseLong(idClient.getSnowflakeDistributedId().getData().toString());
                    son.setCommentId(sonId);
                    son.setVideoId(video.getVideoId());
                    son.setRootId(id);    //插根评论
                    son.setParentId(id);  //插父评论
                    son.setToUserId(274892974465220608L);   //表示根评论用户
                    son.setUserId(275015347524861952L);     //表示评论用户
                    son.setContent("子评论:" + new Random().nextGaussian());
                    son.setLove(new Random().nextInt(3333) + 1);    //3000内随机数
                    son.setBad(new Random().nextInt(77) + 1);    //100内随机数
                    son.setCreateTime(new Date());
                    son.setIsTop(IsFlagEnum.NO.getCode());
                    son.setIsDelete(IsFlagEnum.NO.getCode());
                    son.setIsLike(IsFlagEnum.NO.getCode());
                    son.setTotalCommentSize(0);
                    son.setDeep(1);
                    testMapper.insert(root);
                    testMapper.insert(son);
                }
            }
            return true;
        });
    }

    @Test
    public void addSonData() {
        //查询所有的评论(被分页了100条)
        List<BiliUserComment> biliUserComments = new ArrayList<>();
        for (BiliUserComment biliUserComment : biliUserComments) {
            BiliUserComment son = new BiliUserComment();
            long id = Long.parseLong(idClient.getSnowflakeDistributedId().getData().toString());
            son.setCommentId(id);
            son.setVideoId(biliUserComment.getVideoId());
            son.setRootId(biliUserComment.getCommentId());    //插根评论
            son.setParentId(biliUserComment.getCommentId());  //插父评论
            son.setToUserId(biliUserComment.getUserId());   //表示根评论用户
            son.setUserId(275015347524861952L);     //表示评论用户
            son.setContent("子评论:" + new Random().nextGaussian());
            son.setLove(new Random().nextInt(99) + 1);    //3000内随机数
            son.setBad(new Random().nextInt(7) + 1);    //100内随机数
            son.setCreateTime(new Date());
            son.setIsTop(IsFlagEnum.NO.getCode());
            son.setIsDelete(IsFlagEnum.NO.getCode());
            testMapper.insert(son);
        }

    }

    @Test
    public void addSonComment() {

        for(int i=0;i<2;++i){
            BiliUserComment son = new BiliUserComment();
            long id = Long.parseLong(idClient.getSnowflakeDistributedId().getData().toString());
            son.setCommentId(id);
            son.setVideoId(282997431174430720L);
            son.setRootId(286208196865949700L);    //插根评论
            son.setParentId(286208196870144000L);  //插父评论
            son.setToUserId(274892974465220600L);   //表示根评论用户
            son.setUserId(275015347524861952L);     //表示评论用户
            son.setContent("子评论:" + new Random().nextGaussian());
            son.setLove(new Random().nextInt(99) + 1);    //3000内随机数
            son.setBad(new Random().nextInt(7) + 1);    //100内随机数
            son.setCreateTime(new Date());
            son.setIsTop(IsFlagEnum.NO.getCode());
            son.setIsDelete(IsFlagEnum.NO.getCode());
            testMapper.insert(son);
        }
    }


    @Test
    public void testPage() {
        //分页查询测试
        List<BiliUserComment> biliUserComments = testMapper.pageList(0, 3);
        List<BiliUserComment> biliUserComments1 = testMapper.pageList(3, 3);
        List<BiliUserComment> biliUserComments2 = testMapper.pageList(6, 3);
        System.out.println(biliUserComments);
        System.out.println(biliUserComments1);
        System.out.println(biliUserComments2);
    }

    @Test
    public void testCommentRedisKey() {
        long start = System.currentTimeMillis();
        //1.拿到分页根评论
        List<BiliUserComment> pagedComment =
                testMapper.queryCommentListPaged(282511737964462080L, 0, 30);
        //2.构造评论树 Map<k,pojo> = Map<root1,pojo1>
        //parallel平行处理30个视频,加快速度
        List<CommentTree> trees = pagedComment.stream().parallel().map(comment -> {
            CommentTree commentTree = new CommentTree();
            //拷贝一份tree
            BeanUtils.copyProperties(comment, commentTree);
            //构造树,查询该评论下的所有子评论
            BuildTree(commentTree);
            return commentTree;
        }).collect(Collectors.toList());
        //3.应该返回一个trees,包含30个根评论,其中每个根评论里面的List<>子评论都赋值完毕
        System.out.println(trees.size());
        for (CommentTree tree : trees) {
            System.out.println(tree.toString());
        }
        System.out.println("构建评论树,总耗时：" + (System.currentTimeMillis() - start) + ":ms");
    }

    //构造评论树
    private void BuildTree(CommentTree commentTree) {
        //1.查找该评论下,是否还有子评论
        List<BiliUserComment> sonComments = testMapper.selectList(Wrappers.<BiliUserComment>lambdaQuery()
                .eq(BiliUserComment::getParentId, commentTree.getCommentId()));
        //2.是否还有子评论?
        if (sonComments == null || sonComments.isEmpty()) {
            //没有,返回咯
            return;
        }
        //3.有子评论,怎办？子评论转换为vo
        List<CommentTree> treesOf = sonComments.stream().parallel().map(comment -> {
            CommentTree sons = new CommentTree();
            //拷贝一份tree
            BeanUtils.copyProperties(comment, sons);
            return sons;
        }).collect(Collectors.toList());
        //set一下
        commentTree.setReplies(treesOf);
        //每个子评论,递归执行一下
        treesOf.stream().parallel().forEach(this::BuildTree);
    }

    @Test
    public void testCommentApi() {
//        List<Long> videoIds = new ArrayList<>();
//        videoIds.add(282527299100737536L);
//        videoIds.add(282617275427586048L);
//        List<Map<String, Object>> biliUserComments = testMapper.queryCommentsByVideoIdWithHot(videoIds, 0.65, 0.35);
//        System.out.println(biliUserComments);
//        System.out.println(biliUserComments.size());
    }

}
